package com.erp.erp_ui.interceptor;

import com.github.pagehelper.PageHelper;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
 * @ClassName PageInterceptor
 * @Description 分页拦截器，只对以ByPage结尾的进行分页处理
 * @Author songjizhen
 * @Date 2020/6/17 14:02
 * @Version 1.0
 */
public class PageInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        String url=httpServletRequest.getServletPath();
        if(url.endsWith("ByPage")){//url.endsWith("ByPage")
            Map<String, String[]> map = httpServletRequest.getParameterMap();
            if(map.containsKey("page")){
               //layui.table中开启page后参数
                PageHelper.startPage(Integer.parseInt(map.get("page")[0]),Integer.parseInt(map.get("limit")[0]));
            }else{
                PageHelper.startPage(Integer.parseInt(map.get("pageNum")[0]),Integer.parseInt(map.get("pageSize")[0]));
            }

        }else{
            PageHelper.startPage(0,1000000);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println(o.toString());
    }

}
